<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="com.jungo.web.bean.Favorite" %>
<%@ page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page import="java.io.File" %>
<%@	taglib prefix="c"			uri="http://java.sun.com/jsp/jstl/core"%>
<%@	taglib prefix="fmt"		uri="http://java.sun.com/jsp/jstl/fmt"%>
<%@	taglib prefix="fn"		uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="spring"	uri="http://www.springframework.org/tags" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.css" />
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
$(document).bind("mobileinit", function () {
    $.mobile.ajaxEnabled = false;
});
</script>
<script src="http://code.jquery.com/mobile/1.3.1/jquery.mobile-1.3.1.min.js"></script>
<title>중고마트</title>
</head>
<body>
<%
	List<Favorite> favorites = (List<Favorite>)request.getAttribute("favorites");
	boolean hasFavorite = false;

	DateFormat df = new SimpleDateFormat("yyyy년 MM월 dd일(HH시 mm분)");
	
	List<Favorite> gloveses = new ArrayList<Favorite>();
	List<Favorite> instruments = new ArrayList<Favorite>();
	List<Favorite> books = new ArrayList<Favorite>();
	for(Favorite favorite : favorites) {
		favorite.setStr_dt(df.format(favorite.getReg_dt()));
		if ( "gloves".equals(favorite.getCategory()) ) {
			gloveses.add(favorite);
		} else if ( "instrument".equals(favorite.getCategory()) ) {
			instruments.add(favorite);
		} else {
			books.add(favorite);
		}
		hasFavorite = true;
	}
%>
<section id="favorite" data-role="page">
    <header data-role="header" data-position="fixed">
    	<a href="index.do" data-role="button" data-icon="home" data-iconpos="notext" data-inline="true">Home</a>
    	<a data-role="button" href="logout.do" class="ui-btn-right" data-ajax="false">로그아웃</a>
    	<h1 style="text-overflow:clip;margin-left:10%;margin-right:10%">내 관심상품</h1>
    </header>
    <div data-role="content">
        <div data-role="collapsible-set" data-theme="c" data-content-theme="d" data-inset="true">
        
    		<c:if test="<%= hasFavorite == false %>">
    		등록한 관심상품이 존재하지 않습니다.
    		</c:if>
    		
        	<c:set var="size" value="<%= gloveses.size() %>"/>
        	<c:if test="${ size != 0 }">
        	<div id="gloves" data-role="collapsible">
		    <h2>중고 야구 글러브</h2>
		    <ul data-role="listview">
        	<c:forEach var="gloves" items="<%= gloveses %>">
		       	<li><a href="favorite.do?fid=${ gloves.fid }" data-rel="external">
             		<h3>키워드: ${ gloves.title } </h3>
             		<p>등록일: ${ gloves.str_dt }</p>
             		<p>희망가격: ${ gloves.min_price }원~${ gloves.max_price }원</p>
             		</a>
               	</li>
        	</c:forEach>
        	</ul>
        	</div>
        	</c:if>
        	
        	<c:set var="size" value="<%= instruments.size() %>"/>
        	<c:if test="${ size != 0 }">
	    	<div id="instrument" data-role="collapsible">
		    <h2>중고 악기</h2>
		    <ul data-role="listview">
		    <c:forEach var="instrument" items="<%= instruments %>">
		    	<li><a href="favorite.do?fid=${ instrument.fid }" data-rel="external">
             		<h3>키워드: ${ instrument.title } </h3>
             		<p>등록일: ${ instrument.str_dt }</p>
             		<p>희망가격: ${ instrument.min_price }원~${ instrument.max_price }원</p>
             		</a>
               	</li>
        	</c:forEach>
		    </ul>
	    	</div>
	    	</c:if>
	    	
	    	<c:set var="size" value="<%= books.size() %>"/>
        	<c:if test="${ size != 0 }">
	    	<div id="book" data-role="collapsible">
		    <h2>중고 책</h2>
		    <ul data-role="listview">
		    <c:forEach var="book" items="<%= books %>">
		    	<li><a href="favorite.do?fid=${ book.fid }" data-rel="external">
             		<h3>키워드: ${ book.title } </h3>
             		<p>등록일: ${ book.str_dt }</p>
             		<p>희망가격: ${ book.min_price }원~${ book.max_price }원</p>
             		</a>
               	</li>
        	</c:forEach>
		    </ul>
	    	</div>
	    	</c:if>
	    	
		</div>
    </div>
    
    <div data-role="content">
        <a id="registerFavorite" href="registerFavorite.do" data-role="button" data-ajax="false">관심상품 등록</a>
    </div>

    <footer data-role="footer" data-position="fixed">
    	<div data-role="navbar" data-iconpos="left">
			<ul>
				<li><a id="products" href="products.do" data-icon="grid" rel="external">전체 상품</a></li>
				<li><a id="myProducts" href="myProducts.do" data-icon="bars" rel="external">내 상품</a></li>
				<li><a id="myFavorites" href="myFavorites.do" data-icon="star" rel="external">내 관심상품</a></li>
			</ul>
		</div><!-- /navbar -->
    </footer>
</section>
</body>
<script>
$(document).bind('pageinit', function() {
	var sectionId = $('section').attr('id');
	
	if ( sectionId == "favorite" ) {
		$('#myFavorites').addClass("ui-btn-active");
	} else if ( sectionId == "products" ) {
		$('#products').addClass("ui-btn-active");
	} else {
		$('#myProducts').addClass("ui-btn-active");
	}
});
</script>
</html>